package com.project.nimsen.web.sys;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.nimsen.dto.UseDto;
import com.project.nimsen.entity.User;
import com.project.nimsen.service.IUserService;
import com.project.nimsen.util.WebUtil;
import com.project.jdbc.utils.DateUtils;
import com.project.jdbc.utils.MD5Encrypt;
import com.project.jdbc.utils.Page;
import com.project.jdbc.web.BaseMVCAction;
/**
 * 用户Control
 * */
@Controller
@RequestMapping(value ="/sys/user")
public class UserControl extends BaseMVCAction {
	@Resource
	WebUtil webUtil;
	@Resource
	IUserService userService;
	
	@RequestMapping(value ="/ajaxUserName")
	@ResponseBody
	public List<Map<String, Object>> ajaxUserName(){
		List<Map<String, Object>> list = null;
		String name = getParameter("name");
		try {
			list = userService.findAjaxUser(name);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return list;
	}
	/**
	 * 跳转页面
	 * */
	@RequestMapping(value ="/list")
	public String list(){
		return "/WEB-INF/sys/user/list";
	}
	/**
	 * 查询方法
	 * */
	@RequestMapping(value="search")
	@ResponseBody
	public Map<String, Object> search(){
		//分页信息
		Page page = new Page(getIntegerParameter("page"));
		page.setPageSize(getIntegerParameter("rows"));
		//返回JSON信息
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", 0);
		UseDto dto = new UseDto();
		dto.setPage(page);
		try {
			List<Map<String, Object>> rows = userService.find(dto);
			if(CollectionUtils.isEmpty(rows)){
				map.put("rows", "");
				return map;
			}
			//转换时间
			for(Map<String, Object> row : rows){
				Date createTime = (Date)row.get("regtime");
				row.put("regtime", DateUtils.convertDate2String(createTime));
			}
			map.put("rows", rows);
			map.put("total", page.getTotalCount());
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 验证用户是否存在
	 * */
	@RequestMapping(value ="/validate")
	@ResponseBody
	public Map<String, Object> validate(){
		Map<String,Object> map = new HashMap<String,Object>();
		//username
		try {
			String username = getParameter("username");
			String id = getParameter("id");
			User use = userService.findUserByName(username);
			
			boolean validate = true;
			if(StringUtils.isBlank(id))
				validate = use==null;
			else
				validate = id.equals(use.getId());
			
			if(!validate)
				map.put("message", "已经存在名称为"+username+"的用户!");
			else
				map.put("message", "");
			map.put("success", validate);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return map;
	}
	/**
	 * 添加一个用户
	 * */
	@RequestMapping(value ="/add")
	public void add(HttpServletRequest request,HttpServletResponse response){
		User use = webUtil.getCurrUser();
		
		UseDto dto = new UseDto();
		dto.setUsername(getParameter("username"));//用户名
		dto.setMail(getParameter("mail"));//邮箱
		dto.setPassword(getParameter("password"));//密码
		dto.setSitename(getParameter("sitename"));//站点名称
		dto.setCurrentuseid(use.getId());
		try {
			if(userService.saveByDto(dto))
				response.getWriter().println(true);
			else
				response.getWriter().println(false);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			try {
				response.getWriter().println(false);
			} catch (IOException e1) {
				e1.printStackTrace();
				logger.error(e1);
			}
		}
	}
	//
	/**
	 * 修改密码
	 * */
	@RequestMapping(value ="/upassword")
	public void upassword(HttpServletRequest request,HttpServletResponse response){
		User use = webUtil.getCurrUser();
		
		String id = getParameter("id");//id
		if(StringUtils.isBlank(id))
			id = use.getId();
		try {
			User upuse = userService.findUserById(id);
			String password = getParameter("password");
			if(StringUtils.isBlank(password)){
				response.getWriter().println(false);
				return;
			}
			upuse.setPassword(MD5Encrypt.encoderByMd5With32Bit(password));
			userService.update(upuse);
			response.getWriter().println(true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			try {
				response.getWriter().println(false);
			} catch (IOException e1) {
				e1.printStackTrace();
				logger.error(e1);
			}
		}
	}
	/**
	 * 删除一个用户
	 * */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		String id = getParameter("id");
		try {
			String errorInfo = userService.deleteById(id);
			if(errorInfo!=null){
				map.put("success", false);
				map.put("message", errorInfo);
				return map;
			}
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return map;
	}
}
